﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BLL;
using Entities;


namespace CuaHangBangDia.QLBD
{
    public partial class QLBDForm : Form
    {
        int item = 15;
        int page = 1;
        //bool _selectAll = false;
        int tong;
        public QLBDForm()
        {
            InitializeComponent();
            dgvBangDia.AutoGenerateColumns = false;
        }

        private void QLBDForm_Load(object sender, EventArgs e)
        {
            gbPhanTrang.Visible = false;
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            txtTenBD.Text = "";
            gbPhanTrang.Visible = true;
            LoadBangDia(getPage(1));
            txtTrang.Text = Convert.ToString(1);
            page = 1;
        }

        private void LoadBangDia(List<BangDia> ds)
        {
            dgvBangDia.DataSource = ds;
            List<LoaiBangDia> loai = LoaiBangDia.Objects.GetAll().ToList();
            List<TheLoai> Theloai = TheLoai.Objects.GetAll().ToList();
            dgcLoaiDia.DataSource = loai;
            dgcTheLoai.DataSource = Theloai;
            dgcLoaiDia.DisplayMember = "TenLoai";
            dgcTheLoai.DisplayMember = "TenTheLoai";
            dgcLoaiDia.ValueMember = "MaLoai";
            dgcTheLoai.ValueMember = "MaTheLoai";
            tong = TongTrang(BangDia.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;

        }
        public List<BangDia> getPage(int page)
        {
            var query = from c in BangDia.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        public static int TongTrang(int soluongdl, int num)
        {
            int kq = (int)(soluongdl / num);
            if (kq * num == soluongdl)
                return kq;
            return kq + 1;
        }

        private void btnLui_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                txtTrang.Text = Convert.ToString(page);
                LoadBangDia(getPage(page));
            }
        }

        private void btnTien_Click(object sender, EventArgs e)
        {
            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrang.Text = Convert.ToString(page);
                LoadBangDia(getPage(page));
            }
        }

        //private void dgvBangDia_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        //{
        //    int n = dgvBangDia.Rows.Count;
        //    if (e.ColumnIndex == 0 && n > 0)
        //    {
        //        _selectAll = !_selectAll;
        //        for (int idx = 0; idx < n; idx++)
        //        {
        //            dgvBangDia.Rows[idx].Selected = _selectAll;
        //            dgvBangDia.Rows[idx].Cells[0].Value = _selectAll;
        //        }

        //    }
        //}

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            string ten = txtTenBD.Text;
            if (ten != "")
            {
                List<BangDia> list = BangDia.Objects.Find(b => b.TenBangDia.Contains(ten)).ToList();
                if (list.Count > 0)
                {
                    LoadBangDia(list);
                    gbPhanTrang.Visible = false;
                }
                else
                {
                    MessageBox.Show("Không tìm thấy băng đĩa có thông tin là : " + ten.ToLowerInvariant(), "Thông Báo");
                    LoadBangDia(list);
                }
            }
            else
                btnLoad.PerformClick();
        }

        private void txtTrang_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadBangDia(getPage(t));
                        txtTrang.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvBangDia.Rows.Count; i++)
            {
                dgvBangDia.Rows[i].Cells[0].Value = false;
            }

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            bool kq = false;
            List<BangDia> list = LayBangDia();
            if (list.Count > 0)
            {
                DialogResult que = MessageBox.Show("Bạn chắc chắn muốn Xóa " + list.Count + " băng đĩa đã chọn?", "Cảnh Báo",
                               MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (que == DialogResult.OK)
                {
                    foreach (BangDia item in list)
                    {
                        kq = BangDia.Objects.Delete(item);
                    }
                    if (kq)
                    {
                        MessageBox.Show("Xóa băng đĩa thành công !");
                        btnTimKiem.PerformClick();
                    }
                }
                else
                {
                    for (int i = 0; i < dgvBangDia.Rows.Count; i++)
                    {
                        dgvBangDia.Rows[i].Cells[0].Value = false;
                    }
                }
            }
            else
                MessageBox.Show("Vui lòng chọn đĩa muốn Xóa ");
        }

        private List<BangDia> LayBangDia()
        {
            List<BangDia> list = new List<BangDia>();
            if (dgvBangDia.Rows.Count > 0)
            {
                for (int i = 0; i < dgvBangDia.Rows.Count; i++)
                {
                    try
                    {
                        if (dgvBangDia.Rows[i].Cells[0].Value != null && dgvBangDia.Rows[i].Cells[0].Value.ToString() == "True")
                        {
                            BangDia b = GetDataBD(i);
                            list.Add(b);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            return list;
        }
        private BangDia GetDataBD(int i)
        {
            BangDia bd = new BangDia();
            bd.MaBangDia = Convert.ToInt32(dgvBangDia.Rows[i].Cells[1].Value.ToString());
            bd.TenBangDia = dgvBangDia.Rows[i].Cells[2].Value.ToString();
            bd.MaLoai = dgvBangDia.Rows[i].Cells[3].Value.ToString();
            bd.MaTheLoai = dgvBangDia.Rows[i].Cells[4].Value.ToString();
            bd.Gia = Convert.ToDouble(dgvBangDia.Rows[i].Cells[5].Value.ToString());
            bd.SoLuong = Convert.ToInt32(dgvBangDia.Rows[i].Cells[6].Value.ToString());
            return bd;
        }

        private void btnTDau_Click(object sender, EventArgs e)
        {
            if (page > 1)
            {
                LoadBangDia(getPage(1));
                txtTrang.Text = Convert.ToString(1);
                page = 1;
            }
        }

        private void btnTCuoi_Click(object sender, EventArgs e)
        {
            if (page < tong)
            {

                LoadBangDia(getPage(tong));
                txtTrang.Text = Convert.ToString(tong);
                page = tong;
            }
        }

        private void txtTenBD_TextChanged(object sender, EventArgs e)
        {
            List<BangDia> list = new List<BangDia>();
            string ten = txtTenBD.Text;
            {
                list = BangDia.Objects.Find(b => b.TenBangDia.Contains(ten)).ToList();
                if (list.Count > 0)
                {
                    LoadBangDia(list);
                    gbPhanTrang.Visible = false;
                }
            }
            if (ten == "")
            {
                list = null;
                LoadBangDia(list);
                gbPhanTrang.Visible = false;
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            bool kq = false;
            List<BangDia> list = LayBangDia();
            if (list.Count > 0)
            {
                DialogResult que = MessageBox.Show("Bạn chắc chắn muốn Cập Nhật lại thông tin của " + list.Count + " băng đĩa đã chọn?", "Cảnh Báo",
                               MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (que == DialogResult.OK)
                {
                    foreach (BangDia item in list)
                    {
                        kq = BangDia.Objects.Update(item);
                    }
                    if (kq)
                    {
                        MessageBox.Show("Cập nhật băng đĩa thành công !");
                        btnTimKiem.PerformClick();
                    }
                }
                else
                {
                    for (int i = 0; i < dgvBangDia.Rows.Count; i++)
                    {
                        dgvBangDia.Rows[i].Cells[0].Value = false;
                    }
                }
            }
            else
                MessageBox.Show("Vui lòng chọn đĩa muốn Cập Nhật ");
        }

        private void dgvBangDia_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int newInteger;
            if (KTSo(e))
            {

                if ((!int.TryParse(e.FormattedValue.ToString(), out newInteger) && !String.IsNullOrWhiteSpace(e.FormattedValue.ToString())))
                {
                    dgvBangDia.Rows[e.RowIndex].ErrorText =
           "Số lượng chỉ nhập dữ liệu kiểu số !";
                    e.Cancel = true;
                }
            }
        }
        private bool KTSo(DataGridViewCellValidatingEventArgs e)
        {
            if (dgvBangDia.Columns[e.ColumnIndex].Name == "dgcGia" ||
                dgvBangDia.Columns[e.ColumnIndex].Name == "dgcSoLuong")
                return true;
            return false;
        }

        private void dgvBangDia_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dgvBangDia.Rows[e.RowIndex].ErrorText = "";
        }
        

    }
}
